package com.tencent.ncnnyolox.ui

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tencent.ncnnyolox.R
import com.tencent.ncnnyolox.adapter.CommonAdapter
import com.tencent.ncnnyolox.basemodule.base.BaseActivity
import com.tencent.ncnnyolox.basemodule.base.BaseDialog
import com.tencent.ncnnyolox.basemodule.event.SingleLiveEvent
import com.tencent.ncnnyolox.basemodule.router.RouterPath
import com.tencent.ncnnyolox.basemodule.utils.CommonLog
import com.tencent.ncnnyolox.basemodule.utils.LiveEventKey
import com.tencent.ncnnyolox.basemodule.utils.MMKey
import com.tencent.ncnnyolox.basemodule.utils.MMkvUtils
import com.tencent.ncnnyolox.basemodule.view.CommonEditDialog
import com.tencent.ncnnyolox.databinding.ActivityCommonWordBinding
import com.tencent.ncnnyolox.net.ApiViewModel
import com.tencent.ncnnyolox.net.bean.ChatPhraseAddDTO
import com.tencent.ncnnyolox.net.bean.PhraseLabelBean
import com.tencent.ncnnyolox.net.bean.request.ChatHistoryRequest
import com.tencent.ncnnyolox.net.bean.request.OrderByItem
import okhttp3.internal.wait

/**
 * <pre>
 * author : yh
 * e-mail : xxx@xx
 * time   : 2023/12/15
 * desc   :常用语
</pre> *
 */
@Route(path=RouterPath.CommonWord_Activity)
class CommonWordActivity : BaseActivity<ApiViewModel,ActivityCommonWordBinding>(),
    View.OnClickListener {

    private val TAG="CommonWordActivity"
    private var binding: ActivityCommonWordBinding? = null
    private lateinit var adapter:CommonAdapter

    override fun layoutId(): Int {
        return R.layout.activity_common_word
    }

    override fun onResume() {
        super.onResume()


    }

    override fun initView(savedInstanceState: Bundle?) {
        adapter=CommonAdapter(this,this)
        mBinding.title.leftView.setOnClickListener{
            finish()
        }
        mBinding.title.rightView.setOnClickListener{

        }

        LiveEventBus.get<PhraseLabelBean>(LiveEventKey.COMMON_EDIT).observe(this){
            initData()
        }
        LiveEventBus.get<String>(LiveEventKey.COMMON_DELETE).observe(this){
            initData()
        }

        mBinding.btnCommonAdd.setOnClickListener {
            val phraseLabel = viewModel.getPhraseLabel().observe(this){
                CommonEditDialog.Builder(this).setTitle("新增")
                    .setLabel(it.data)
                    .setListener(object :CommonEditDialog.Builder.OnListener{
                        override fun onConfirm(dialog: BaseDialog?, text: String,bean: PhraseLabelBean) {
                            var addDTO:ChatPhraseAddDTO= ChatPhraseAddDTO(MMkvUtils.decodeString(MMKey.memberId),text,
                                bean.labelId.toLong(),"")

                            viewModel.addCommonWord(addDTO).observe(this@CommonWordActivity){it1->
                                if (it1.code==200){
                                    ToastUtils.showShort("添加成功")
                                    initData()
                                    dialog?.dismiss()
                                }else{
                                    ToastUtils.showShort("添加失败")
                                    CommonLog.d("CommonWordActivity",it1.msg)
                                }
                            }
                        }

                        override fun onCancel(dialog: BaseDialog?) {
                            super.onCancel(dialog)
                            dialog?.dismiss()
                        }

                    }).show()
            }
        }

        adapter?.setClickItemListener {
            LiveEventBus.get<String>(LiveEventKey.Common_Word).post(it)
            finish()
        }
    }

    override fun initData() {
        viewModel.getCommonWord(MMkvUtils.decodeString(MMKey.memberId)).observe(this){
            if (it.code==200){

                adapter.setDate(it.data)
                mBinding.recycleCommon.layoutManager=LinearLayoutManager(this)

                mBinding.recycleCommon.adapter=adapter
            }else{
                ToastUtils.showShort(it.msg)
            }
        }


    }

    override fun onClick(v: View?) {

    }
}